<?php
/**
 *	后台管理系统-管理员
 */
namespace app\bews\controller;
use app\AppApi;
use think\facade\Db;
use think\facade\View;
use think\facade\Cookie;
use think\facade\Config;

use app\bews\model\BewAdminConfig;

class Base{
	public $adminId = null;
    //好了 现在能看到我就算全部成功
	public $config = [];
	public function __construct(){
		date_default_timezone_set('PRC');
		# 获取配置
		$BewAdminConfig = new BewAdminConfig();
		$this->config = $BewAdminConfig->getAll();
		# 获取账户，账户判断
		$this->adminId = Cookie::get('admin_id');
		if(empty($this->adminId)){
			header('Location:'.$this->config['admin_route'].'Login/index');
			exit;
		}
		$aUser = Db::table('bew_admin_user')->where('uid',$this->adminId)->find();
		if (empty($aUser)) {
			Cookie::delete('admin_id');
			$this->error('管理员账户不存在');
		}
		if ($aUser['status'] != 1) {
			Cookie::delete('admin_id');
			$this->error('管理员已被禁用');
		}
		# 获取用户组权限
		$group = Db::table('bew_admin_user_group')->where(['group_id'=>$aUser['group_id']])->find();
		if(empty($group)){
			$this->error('对不起，您没有权限');
		}
		# 获取当前链接，查询是否有权限
		$key = trim(input('get.key'));
		if(isset($key)){
			$aMenu = Db::table('bew_admin_sys_menu')->where('src',$key)->find();
		}else{
			# 当前访问控制器和方法
			$controller = request()->controller();
			$action = request()->action();
			$key = $controller.'/'.$action;
			$aMenu = Db::table('bew_admin_sys_menu')->where('src',$key)->find();
		}
		if(empty($aMenu)){
			$this->error('对不起，您访问的功能不存在');
		}
		$rights = json_decode($group['rights']);
		if(!in_array($aMenu['smid'],$rights)){
			$this->error('对不起，您没有权限');
		}
		View::assign([
			'aUser' => $aUser,
			'config' => $this->config
		]);
	}
	/**
	 * 返回json对象
	 */
	protected function returnCode($code,$data=[],$count=10){
		header('Content-type:application/json');
		if($code == 0){
			$arr = array(
				'code'=>$code,
				'msg'=>'成功',
				'count'=> $count,
				'data' => $data
			);
		}else if($code >= 1 && $code <= 100){
			$arr = array(
				'code'	=>	$code,
				'msg'	=>	$data
			);
		}else{
			$appapi = new AppApi();
			$arr = array(
				'code'=>$code,
				'msg'=>$appapi::errorTip($code)
			);
		}
		echo json_encode($arr);
		if($code != 0){
			exit;
		}
	}
}